package com.mw.util;

import com.mw.domain.News;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringTool {
    /**
     * 特定形式的字符串转化为Map
     * @param s "label=value,label=value,label=value,···"形式的字符串
     * @return
     */
    public Map<String,Integer> StringToMap(String s){
        List<Map<String,Integer>> resultlist = new ArrayList<Map<String,Integer>>();
        List<String> stringList = Arrays.asList(s.split(","));
        Map<String,Integer> map = new HashMap<>();
        for(int i=0;i<stringList.size();i++){
            List<String> list = Arrays.asList(stringList.get(i).split("="));
            map.put(list.get(0),Integer.parseInt(list.get(1)));
            resultlist.add(map);
        }
        return map;
    }



    /**
     * Map加List
     * @param map {"AAA":1,"BBB":2,"CCC":3}
     * @param list {BBB,DDD}
     * @param value 同一个Key加多少
     * @return {"AAA":1,"CCC":3,"BBB":3,"DDD":1}
     */
    public Map<String,Integer> MapAddList(Map<String,Integer> map,List<String> list,Integer value){
        for(int i = 0;i<list.size();i++){
            String label = list.get(i).toString();
            if(map.get(label)!=null){
                map.put(label,map.get(label)+value);
            }else{
                map.put(label,value);
            }
        }
        return map;
    }

    /**
     *
     * @param s1 用户喜好字段
     * @param s2 新闻类型字段
     * @param value 加分
     * @return 同类型加分，没有的类型增加。
     */
    public String typeAdd(String s1,String s2,Integer value){
        String s;
        Map<String,Integer> map = StringToMap(s1);
        List<String> list = Arrays.asList(s2.split(","));
        Map<String,Integer> resultmap = MapAddList(map,list,value);
        s = resultmap.toString();
        //去掉头尾{}和所有空格
        s = s.substring(1,s.length());
        s = s.substring(0,s.length()-1);
        s = s.replaceAll(" ","");
        return s;
    }

    public String getShuMingHao(String s){
        Pattern pattern = Pattern.compile("《(.*?)》");
        Matcher matcher = pattern.matcher(s);
        if(matcher.find()) {
            return matcher.group(1);
        }
        return "";
    }

    public Set<String>[] getlabel(List<News> newsList){
        Set<String>[] set = new Set[5];
        set[0] = new HashSet<>();
        set[1] = new HashSet<>();
        set[2] = new HashSet<>();
        set[3] = new HashSet<>();
        set[4] = new HashSet<>();
        for(int i = 0;i<newsList.size();i++){
            String s = newsList.get(i).getType();
            if(s.contains(",")) {
                if (s.contains("明星")) {
                    String[] ss = s.split(",");
                    for (String l : ss) {
                        set[0].add(l);
                    }
                } else if (s.contains("时尚")) {
                    String[] ss = s.split(",");
                    for (String l : ss) {
                        set[1].add(l);
                    }
                } else if (s.contains("影视")) {
                    String[] ss = s.split(",");
                    for (String l : ss) {
                        set[2].add(l);
                    }
                } else if (s.contains("宠物")) {
                    String[] ss = s.split(",");
                    for (String l : ss) {
                        set[3].add(l);
                    }
                } else if (s.contains("生活")) {
                    String[] ss = s.split(",");
                    for (String l : ss) {
                        set[4].add(l);
                    }
                }
            }
        }
        return set;
    }
}
